package org.whereiswally.faint;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

import org.apache.commons.io.IOUtils;

import de.offis.faint.controller.MainController;
import de.offis.faint.model.ImageModel;
import de.offis.faint.model.Region;
import de.offis.faint.recognition.plugins.eigenfaces.EigenfaceRecognition;

public class FaintTest implements Serializable {

    public String test() {
	return "ok";
    }

    /**
     * @param args
     */
    public String run(byte[] bytesWally, byte[] bytesCompareTo) {
	try {

	    String result = "fdsfsd";

	    MainController mc = MainController.getInstance();
	    File wally = new File("wally");
	    IOUtils.write(bytesWally, new FileOutputStream(wally));
	    ImageModel im = new ImageModel(wally);
	    im.getImage(true);
	    Region[] reg = mc.detectFaces(im, false);
	    Region reg0 = reg[0];
	    mc.getFaceDB().put(reg0, "wally");
	    File fCT = new File("fCT");
	    IOUtils.write(bytesCompareTo, new FileOutputStream(fCT));
	    ImageModel im2 = new ImageModel(fCT);
	    im2.getImage(true);
	    Region[] reg_2 = mc.detectFaces(im2, false);
	    Region reg2 = reg_2[0];

	    // mc.getFaceDB().containsRegion(reg0);

	    EigenfaceRecognition efr = new EigenfaceRecognition();

	    result = reg0.toString();

	    wally = null;
	    im = null;
	    reg = null;
	    reg0 = null;
	    // mc.dispose();
	    mc = null;
	    System.gc(); // return

	    return efr.getRecognitionPoints(reg2).toString();

	} catch (Exception e) {
	    StringWriter sw = new StringWriter();
	    e.printStackTrace(new PrintWriter(sw));
	    return "ERROR: " + sw.toString();
	}

    }
}
